/*author stone
******** 通用js *********
**************/
const STONE={
	//获取ip地址
	findIP:function(callback) {
	    var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; //compatibility for firefox and chrome
	    var pc = new myPeerConnection({iceServers: []}),
	        noop = function() {},
	        localIPs = {},
	        ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
	        key;
			
	    function ipIterate(ip) {
	        if (!localIPs[ip]) callback(ip);
	        localIPs[ip] = true;
	    }
	    pc.createDataChannel("");
	    pc.createOffer().then(function(sdp) {
	        sdp.sdp.split('\n').forEach(function(line) {
	            if (line.indexOf('candidate') < 0) return;
	            line.match(ipRegex).forEach(ipIterate);
	        });
	        pc.setLocalDescription(sdp, noop, noop);
	    });
	    pc.onicecandidate = function(ice) {
	        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
	        ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
	    };
	},
	//js调用发送短信接口
	sendInfo:function(phone, content) {
		var u = navigator.userAgent;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		if (isiOS) {
			location.href = "sms://" + phone + "&body=" + content;
		} else {
			location.href = "sms:" + phone + "?body=" + content;
		}
		/************示例************/
		//sendInfo('13008847296', 'hello');
	},
	//js获取元素style
	getStyle:function(ele) {
		var style = null;
	
		if (window.getComputedStyle) {
			style = window.getComputedStyle(ele, null);
		} else {
			style = ele.currentStyle;
		}
		return style;
	},
	//判断平台
	IsPC:function() {
		var userAgentInfo = navigator.userAgent;
		var Agents = ["Android", "iPhone",
			"SymbianOS", "Windows Phone",
			"iPad", "iPod"
		];
		var flag = true;
		for (var v = 0; v < Agents.length; v++) {
			if (userAgentInfo.indexOf(Agents[v]) > 0) {
				flag = false;
				break;
			}
		}
		return flag;
	},
	//创建一个动态随机码
	createCode:function(num) {
		code = '';
		var codeLength = num;
		var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
			'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
		for (var i = 0; i < codeLength; i++) {
			var index = Math.floor(Math.random() * 36);
			code += random[index];
		}
		return code;
		/************示例************/
		//createCode(8);
	},
	//时间格式化
	dateFormat:function(fmt, date) {
		let ret;
		const opt = {
			"Y+": date.getFullYear().toString(), // 年
			"m+": (date.getMonth() + 1).toString(), // 月
			"d+": date.getDate().toString(), // 日
			"H+": date.getHours().toString(), // 时
			"M+": date.getMinutes().toString(), // 分
			"S+": date.getSeconds().toString() // 秒
			// 有其他格式化字符需求可以继续添加，必须转化成字符串
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			};
		};
		return fmt;
		/************示例************/
		//dateFormat("YYYY-mm-dd HH:MM:SS", new Date(res.result.recordset[0].ed))
	},
	dateFormat1:function(date, fmt) {
		var o = {
			"M+": date.getMonth() + 1,
			"d+": date.getDate(),
			"h+": date.getHours(), //小时 
			"m+": date.getMinutes(), //分 
			"s+": date.getSeconds(), //秒 
		};
		if (/(y+)/.test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
		}
		for (var k in o) {
			if (new RegExp("(" + k + ")").test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
			}
		}
		return fmt;
	},
	
	// 判断一个url是否可以访问
	IsLoad:function(url, fun) {
		$.ajax({
			url: url,
			type: 'GET',
			success: function(response) {
				if ($.isFunction(fun)) {
					fun(true);
				}
			},
			error: function() {
				if ($.isFunction(fun)) {
					fun(false);
				}
			}
		});
		/************示例************/
		// IsLoad('同一域下的url地址', function(res) {
		// 	if (res) {
		// 		alert('请求的url可以访问');
		// 	}
		// });
	},
	
	//测试链接连通性
	isRunUrl:function(url) {
		return new Promise(function(resolve, reject) {
			// 测试链接连通性, 主要检测404错误
			// 由于AJAX通常无法区分404和跨域问题
			// 所以只能用script 或者 link标签
			// link比script更容易捕获错误
			var dom = document.createElement('link');
			dom.href = url;
			dom.rel = 'stylesheet';
			document.head.appendChild(dom);
			dom.onload = function() {
				document.head.removeChild(dom);
				resolve();
			}
			dom.onerror = reject;
		});
		/************示例************/
		// isRunUrl("/").then(function(data) {
		// 	//处理resolve的代码
		// 	console.log("Promise被置为resolve", data);;
		// }, function(data) {
		// 	//处理reject的代码
		// 	console.log("程序被置为了reject", data);
		// })
	},
	//获取地址栏参数
	getRequest:function() {   
	   var url = window.location.search; //获取url中"?"符后的字串   
	   var theRequest = new Object();   
	   if (url.indexOf("?") != -1) {   
		  var str = url.substr(1);   
		  strs = str.split("&");   
		  for(var i = 0; i < strs.length; i ++) {   
			  //就是这句的问题
			 theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); 
			 //之前用了unescape()
			 //才会出现乱码  
		  }   
	   }   
	   return theRequest;   
	},
	GetRequest:function() {
		var url = location.search;
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	},
	//创建一个32位随机码
	createCode:function(user_id, n) {
		const source_string = 'E5FCDG3HA4B1NPJ2RSTV67MWX89KLYZ0';
		let num = user_id;
	
	
		let code = '';
	
		while (num > 0) {
	
			let mod = num % 32
	
			num = (num - mod) / 32
	
			code = code + source_string[mod - 1];
		}
		code = (Array(n).join(0) + code).slice(-n).toString().toUpperCase();
		code = code.replace(0, RndNum(1));
		return code;
	
	},
	//获取webservice json
	getWebserviceJson:function(res) {
		// console.log(res.all[0].innerHTML);
		// var jsonx=JSON.parse(res.all[0].innerHTML);
		// console.log(jsonx.Data);
		return $.parseJSON($(res).find('string').html())
	},
}
//通过window.orientation来判断设备横竖屏
// (function(doc, win) {
// 	var docEl = doc.documentElement,
// 		resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
// 		recalc = function() {
// 			var clientWidth = docEl.clientWidth;
// 			if (!clientWidth) return;
// 			if (clientWidth >= 640) {
// 				docEl.style.fontSize = '85px';
// 			} else {
// 				docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
// 			}
// 		};

// 	if (!doc.addEventListener) return;
// 	win.addEventListener(resizeEvt, recalc, false);
// 	doc.addEventListener('DOMContentLoaded', recalc, false);
// })(document, window);
